package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author Wq
 * @since 2022-10-05
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    @Override
    public ResponseResult<WmUser> saveUser(WmUser user) {
        //1、参数校验
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2、唯一性校验
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getApUserId, user.getApUserId());
        WmUser userDB = this.getOne(wrapper);
        if (userDB == null) {
            user.setCreatedTime(new Date());
            this.save(user);
            return ResponseResult.okResult(user);
        } else {
            return ResponseResult.okResult(userDB);
        }
    }

    @Override
    public ResponseResult login(LoginDto dto) {
        //1、参数校验
        String name = dto.getName();
        String passowrd = dto.getPassword();
        if (dto == null || StringUtils.isBlank(name) || StringUtils.isBlank(passowrd)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USERNAME_PASSWORD_NOT_EXISTS);
        }
        //通过用户名查询数据库
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmUser::getName, name);
        WmUser userDB = this.getOne(wrapper);
        //查询不到 返回用户不存在
        if (userDB == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        //比对密码
        String passwordDB = userDB.getPassword();//这是从数据库查出来秘闻
        passowrd = DigestUtils.md5Hex(passowrd + userDB.getSalt());
        // 密码不一致，返回密码错误
        if (!StringUtils.equals(passowrd, passwordDB)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        //登录成功
        HashMap<String, Object> resultMap = new HashMap<>();
        //用户脱敏处理
        userDB.setPassword("");
        userDB.setSalt("");
        userDB.setEmail("");
        userDB.setPhone("");
        resultMap.put("user", userDB);
        //生成token
        Map<String, Object> claimMaps = new HashMap<>();
        claimMaps.put("userId", userDB.getId());
        claimMaps.put("userName", userDB.getName());
        String token = AppJwtUtil.getToken(claimMaps);
        resultMap.put("token", token);
        return ResponseResult.okResult(resultMap);
    }
}
